<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<?php 
	if( !isset($_COOKIE["username"]) ){
		echo "<script>window.location.href='login.php'</script>";
	}else{
		setcookie("username", $_COOKIE["username"],time() + 900);
	}
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Profile Operate</title>
<link href="css/template.css" rel="stylesheet" type="text/css" />
<link href="css/test/ProfileOperate.css" rel="stylesheet" type="text/css" />
<script src="js/droplinemenu.js" type="text/javascript"></script>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript">
//build menu with DIV ID="myslidemenu" on page:
droplinemenu.buildmenu("nav")
</script>

<link href="css/ui-lightness/jquery-ui-1.9.2.custom.css" rel="stylesheet">
<script src="js/jquery-1.8.3.js"></script>
<script src="js/jquery-ui-1.9.2.custom.js"></script>

<script>

	function UpdateMessageContent(){
		var clinicNo = document.getElementById("clinic").value;
		
		var content = jQuery("#content").val();
		var postData = "clinicNo=" + clinicNo + "&content=" + content;
		jQuery.ajax({type:'post', async:false, dataType:'json', url:'message_content_op.php', data:postData, success:function(result){
				if( result.data.status == "success" ){
					alert("update success!");
				}else{
					alert("update fail, unknown cause!");
				}
			},
			error:function(XMLHttpRequest, textStatus, errorThrown){
				alert(XMLHttpRequest.responseText);
			}
		});
	}


	function GetMessageContent(clinicNo){
		var postData = "clinicNo=" + clinicNo;
		jQuery.ajax({type:'post', async:false, url:"get_message_content.php", data:postData, success:function(result){
				jQuery("#content").val(result);
			},
			error:function(XMLHttpRequest, textStatus, errorThrown){
				alert(XMLHttpRequest.responseText);
			}
		});
	}

	


</script>
</head>

<body>
	<div id="container">
		<div id="header"></div>
		<?php include_once 'nav.php';?>
		<?php 
			$ini_array = parse_ini_file("config/config.ini");
			
			$conn = @mysqli_connect($ini_array["host"], $ini_array["username"], $ini_array["password"], $ini_array["db_name"]) or die("cann't connect the mysql db");
			mysqli_set_charset($conn, "utf8");
			ini_set('default_charset', "UTF-8");
			$getClinicSql = "select Clinic_no, Clinic_name, Clinic_address from clinic";
			$getClinicResult = mysqli_query($conn, $getClinicSql);
			
			$fp = fopen("./config/clinic_content/default.txt", "r");
			$content = fread($fp, filesize("./config/clinic_content/default.txt"));
		?>
		<div id="main_content" style="text-align:center;margin:0 auto;width:400px;">
			<table>
				<tr>
					<td style="text-align:right;"><label>Clinic</label></td>
					<td style="text-align:left;">
						<select id="clinic" onchange="GetMessageContent(this.value)">
							<option value="0">default</option>
							<?php 
								while( $row = mysqli_fetch_array($getClinicResult)){
							?>
							<option value="<?php echo $row["Clinic_no"]?>"><?php echo $row["Clinic_name"]?></option>
							<?php 
								}
							?>
						</select>
					</td>
				</tr>
				<tr>
					<td style="text-align:right;"><label>content</label></td>
					<td style="text-align:left;"><textarea rows="20" cols="40" id="content"><?php echo $content?></textarea></td>
				</tr>	
				
				<tr>
					<td></td>
					<td style="text-align:left;"><input type="button" value="update" onclick="UpdateMessageContent()" /></td>
				</tr>
				<tr>
					<td></td>
					<td style="text-align:left;"><label>Tip:please keep these tag:&lt;time&gt;,&lt;date&gt; and &lt;location&gt;. They will be replaced with the specified infomation.</label></td>
				</tr>
			</table>
		</div>
		<?php include_once 'footer.php';?>
	</div>
</body>
</html>
